#include "LinkTree.h"
#include <iostream>

TreeNode::TreeNode(const ElementType &element)
{
    data = element;
    parent = nextSbling = firstChild = nullptr;
}

void TreeNode::Connect(TreeNode *parent)
{
    if(!parent)
        return;
    this->parent = parent;
    
    this->nextSbling = parent->firstChild;
    parent->firstChild =this;
}

void TreeNode::Disconnect(TreeNode *parent)
{
    if(parent == nullptr && this->parent != parent)
        return;
    
    TreeNode *travelPoint = parent->firstChild;
    if(this == parent->firstChild)//我是双亲的第一个孩子
    {
        parent->firstChild = this->nextSbling;
    }
    else
    {
        while(travelPoint->nextSbling != nullptr)
        {
            if(travelPoint->nextSbling == this)
            {
                travelPoint->nextSbling = this->nextSbling;
                break;
            }
            travelPoint = travelPoint->nextSbling;
        }
    }
    this->parent = this->nextSbling = nullptr;
}

LinkTree::LinkTree()
{
    root = new TreeNode(nullptr);
    Free = nullptr;
    Print = nullptr;
    Compare = nullptr;
}

LinkTree::~LinkTree()
{
    FreeNode(root);
}

DoubleLinkList LinkTree::FindByElement(const ElementType &element)
{
    DoubleLinkList list;
    FindNode(root,element,list);
    return list;
}



int LinkTree::GetTreeHeight()
{
    return GetNodeHeight(root) - 1;
}

int LinkTree::Size()
{
    int count = 0;
    GetNodeSize(root,count);
    return count - 1;
}

int LinkTree::LeafSize()
{
    int count = 0;
    GetNodeLeafSize(root,count);
    return count;
}

void LinkTree::show()
{
    show(root,-1);
}

void LinkTree::GetNodeLeafSize(TreeNode *node, int &count)
{
    if(node == nullptr)
    {
        return;
    }
    if(node->firstChild == nullptr)
        count++;
    GetNodeLeafSize(node->firstChild,count);
    GetNodeLeafSize(node->nextSbling,count);
}

void LinkTree::GetNodeSize(TreeNode *node, int &count)
{
    if(node == nullptr)
        return;
    count++;
    GetNodeSize(node->firstChild,count);
    GetNodeSize(node->nextSbling,count);
}

int LinkTree::GetNodeHeight(TreeNode *node)
{
    if(node == nullptr)
        return 0;
    int MaxHeight = 0;
    TreeNode *travelPoint = node->firstChild;
    while(travelPoint != nullptr)
    {
        int NodeHeight = GetNodeHeight(travelPoint);//递归计算子节点高度
        MaxHeight = MaxHeight > NodeHeight ? MaxHeight : NodeHeight;
        travelPoint = travelPoint->nextSbling;
    }
    return MaxHeight + 1;
}

void LinkTree::FindNode(TreeNode *node, const ElementType &element, DoubleLinkList &list)
{
    if(node == nullptr)
        return;
    if(Compare)
    {
        if(node != root && Compare(node->data,element) )
        {
            list.InsertTail(node);
        }
    }
    else if(node->data == element && node != root)
    {
        list.InsertTail(node);
    }
    FindNode(node->firstChild,element,list);
    FindNode(node->nextSbling,element,list);

}

void LinkTree::show(TreeNode *node, int deepth)
{
    if(node == nullptr)//终止条件
        return;
    
    if(node->parent !=nullptr)//不是根节点
    {
        if(deepth >0)
        {
        for(int i = 0; i <deepth-1;i++)
            std::cout<<"    ";
        if(node->nextSbling != nullptr)
            std::cout<<"├── ";
        else
            std::cout<<"└──  ";
        }

        Print(node->data);
    }
    
    //深度优点遍历
    show(node->firstChild,deepth+1);
    show(node->nextSbling,deepth);

    //广度优先
    // show(node->nextSbling);
    // show(node->firstChild);
}

void LinkTree::FreeNode(TreeNode *node)
{
    if(node == nullptr)
    {
        return;
    }
    if(Free)
        Free(node->data);
    FreeNode(node->firstChild);
    FreeNode(node->nextSbling);
    delete node;
}
